package com.xdy.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * User: xuedaiyao
 * Date: 2017/9/6
 * Time: 11:11
 * Description:
 */

@RestController
@EnableScheduling
public class Producer {
    //新版本的jsmTemplate同时支持queue和topic发送
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @RequestMapping("/sendMsg")
    public void send(String msg) {
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

    @RequestMapping("/sendTopic")
    public void sendTopic(String msg) {
        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }

    @Scheduled(fixedDelay=3000)
    public void scheduleSend(){
        this.jmsMessagingTemplate.convertAndSend(this.queue,"hi,这是定时消息！（queue）");
        this.jmsMessagingTemplate.convertAndSend(this.topic,"hi,这是定时消息！（topic）");
    }
}

